﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace thailandProject.Models
{
    /// <summary>
    /// Contains functions to receive client queries and sort the list
    /// </summary>
    public class ClientSort
    {

        public static PagedList<CLIENT> getSortedClientList(string clientName, string sortType, int pageSize, int pageNumber)
        {
            PagedList<CLIENT> result = new PagedList<CLIENT>();
            try
            {
                List<CLIENT> clientList;
                using (ThailandEntities a = new ThailandEntities())
                {
                    var clients = from client in a.CLIENTs
                                  where client.clientIsActive == true &&
                                        client.clientFirstName.StartsWith(clientName) ||
                                        client.clientLastName.StartsWith(clientName) ||
                                        (client.clientFirstName + " " + client.clientLastName).StartsWith(clientName)
                                  select client;

                    //NOTE: If we change the sortable columns, then add/remove it here
                    switch (sortType)
                    {
                        case "clientFirstName": clients = clients.OrderBy(c => c.clientFirstName); break;
                        case "clientLastName": clients = clients.OrderBy(c => c.clientLastName); break;
                        case "clientCity": clients = clients.OrderBy(c => c.clientCity); break;
                        case "clientEmail": clients = clients.OrderBy(c => c.clientEmail); break;
                    }

                    clientList = clients.ToList();
                }
                result = PagedList<CLIENT>.toPagedList(clientList, pageSize, pageNumber);
            }
            catch (Exception ex)
            {
                ErrorLog.logError(ex);
            }

            return result;
        }
    }
}